﻿// <copyright file="ActionExtensions.cs" company="Ucaya">
// </copyright>
// <author>Ucaya</author>

namespace KinectExtensions
{
    #region

    using System;
    using System.Collections.Generic;
    using System.Windows.Threading;

    #endregion

    public static class ActionExtensions
    {
        #region Constants and Fields

        private static readonly List<DispatcherTimer> Timers = new List<DispatcherTimer>();

        #endregion

        #region Public Methods

        public static void Delay(this Action action, double ms)
        {
            action.Delay(TimeSpan.FromMilliseconds(ms));
        }

        public static void Delay(this Action action, TimeSpan ts)
        {
            Dispatcher dispatcher = Dispatcher.CurrentDispatcher;
            var timer = new DispatcherTimer();
            timer.Interval = ts;
            Timers.Add(timer);
            timer.Tick += delegate
                {
                    timer.Stop();
                    dispatcher.BeginInvoke(action);
                    Timers.Remove(timer);
                };
            timer.Start();
        }

        #endregion
    }
}